/**
 * Created with JetBrains PhpStorm.
 * User: AliRiza
 * Date: 21.12.2012
 * Time: 23:28
 * To change this template use File | Settings | File Templates.
 */
window.onload = (function(){
    try{
        $("#shorten_form").submit(function(event) {
            var sourceUrl=$('#urlBox').val();
            event.preventDefault();
            if($('#urlBox').val() == '')
                return false;
            var $form = $( this),
                createUrl = $form.attr( 'action' );
            $.post('getMetaTag.php', $form.serialize().replace(/&/g,'%26'),
                function(data){
                    var response;
                    try{
                        response = jQuery.parseJSON(data);
                    }
                    catch(e){
                        alert(e);
                    }

                    if(!(response.Title == "" && response.Description == "")){
                        $('.gbox').slideDown('fast');
                        $('.gboxtop').show();
                    }
                    $('#title','.gbox').html("<h2><a href='"+sourceUrl+"'>"+response.Title+"</a></h2>");
                    $('#description','.gbox').html("<h3>"+response.Description+"</h3>");
                    $('#url','.gbox').html("<h3>"+sourceUrl+"</h3>");
                }
            );
            $.post( createUrl, $form.serialize().replace(/&/g,'%26'),
                function( data ) {
                    var response = jQuery.parseJSON(data);
                    $("#inputForm").slideDown('slow');
                    $('#share').fadeIn('slow');
                    $('#urlBox').fadeIn('slow').val(response.link).select();
                    $('#clearShortLink').fadeIn('slow');
                    $('input[type=submit]','#inputForm').hide();
                    $('input[type=button]','#inputForm').show();
                    $('#qrcode').qrcode({
                        render  : 'table',
                        text	: response.link
                    });
                    $.fx.speeds._default = 1000;
                    $("#qrcode").dialog({
                        autoOpen: false,
                        show: "blind",
                        hide: "explode",
                        resizable: false,
                        draggable: false,
                        modal:true,
                        title: response.link,
                        width: 281
                    });
                    if(response.clickCount != '0' && response.clickCount != ''){
                        $('#clickCount','.gbox').text("Clicked " + response.clickCount + " times");
                    }
                    else{
                        $('#clickCount','.gbox').text("Clicked 0 times");
                    }
                }
            );
        });
    }catch(e){}});

function createNewLinkFunc(){
    $('#share').fadeOut('slow');
    $('#urlBox').fadeIn('slow').val("");
    $('#clearShortLink').fadeOut('slow');
    $('input[type=submit]','#inputForm').show();
    $('input[type=button]','#inputForm').hide();
    $('#qrcode').empty();
    $("#inputForm").slideDown('slow');
    $('.gbox').slideUp('fast');
    $('.gboxtop').hide();
}

function openQRCode(){
    $("#qrcode").dialog("open");
}

function copyToClipboard(s) {
    // ie
    if (window.clipboardData && clipboardData.setData) {
        clipboardData.setData('text', s);
    }
    // others
    else {
        var flashcopier = 'flashcopier';
        if(!document.getElementById(flashcopier)) {
            var divholder = document.createElement('div');
            divholder.id = flashcopier;
            document.body.appendChild(divholder);
        }
        document.getElementById(flashcopier).innerHTML = '';
        var divinfo = '<embed src="clipboard.swf" FlashVars="clipboard='+encodeURIComponent(s)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
        document.getElementById(flashcopier).innerHTML = divinfo;
    }
}

function copyIntoClipboard(text) {

    var flashId = 'flashId-HKxmj5';

    /* Replace this with your clipboard.swf location */
    var clipboardSWF = 'clipboard.swf';

    if(!document.getElementById(flashId)) {
        var div = document.createElement('div');
        div.id = flashId;
        document.body.appendChild(div);
    }
    document.getElementById(flashId).innerHTML = '';
    var content = '<embed src="' +
        clipboardSWF +
        '" FlashVars="clipboard=' + escape(text) +
        '" width="0" height="0" type="application/x-shockwave-flash"></embed>';
    document.getElementById(flashId).innerHTML = content;
}